package com.eqdd.yiqidian.ui.tongxunlu.stafftree

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.widget.RecyclerView
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.base.AppConstent
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.DepartmentStaffBean
import com.eqdd.yiqidian.bean.DepartmentStaffTreeBean
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonCallBack
import com.jhj.datalibrary.interfaces.OnCustomTopbarListener
import com.jhj.datalibrary.tree.single.BaseSingleListAdapter
import com.jhj.datalibrary.tree.single.BaseSingleTreeActivity
import com.jhj.datalibrary.tree.single.BaseSingleTreeAdapter
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.layout_title.view.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.toast
import java.util.*

/**
 * 不带checkbox的树型数据点击事件，进入新的界面
 * </p>
 * 对非理想的数据进行格式化
 * Created by jhj on 18-5-18.
 */

class StaffTreeSingleActivity : BaseSingleTreeActivity<DepartmentStaffTreeBean>() {

    companion object {
        const val TYPE_TONGXUNLU = "type_tong_xun_lu"//通讯录中的同事
        const val TYPE_DEPARTMENT_DIARY = "type_department_diary"//查看指定部门员工，查看其日志，
        const val TYPE_SINGLE_SELECT = "type_single_selected" //选择返回该员工信息
    }


    override val adapter: BaseSingleTreeAdapter<DepartmentStaffTreeBean, out RecyclerView.ViewHolder>
        get() = StaffTreeSingleAdapter(this)
    override val mAdapter: BaseSingleListAdapter<DepartmentStaffTreeBean, out RecyclerView.ViewHolder>
        get() = StaffTreeSingleListAdapter(this)

    @SuppressLint("MissingSuperCall")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val mTitle = intent.getStringExtra(Config.TITLE) ?: "同事"
        (listAdapter as StaffTreeSingleListAdapter).type = intent.getStringExtra(Config.TYPE)
        (treeAdapter as StaffTreeSingleAdapter).type = intent.getStringExtra(Config.TYPE)
        val departId = intent.getIntExtra(Config.ID, 0)
        if (departId == -1) {
            toast("部门下没有员工")
            finish()
            return
        }
        initTopBar(R.layout.layout_title, object : OnCustomTopbarListener {
            override fun onLayout(view: View) {
                view.topBar_title.text = mTitle
                view.topBar_back.onClick {
                    finish()
                }
            }
        })


        OkGo.post<HttpResult<List<DepartmentStaffBean>>>(HttpConfig.BASE_URL + HttpConfig.GET_STAFF_TREE)
                .params("companyId", AppConstent.user.getCompanyId())
                .params("ParentId", departId)
                .execute(object : JsonCallBack<HttpResult<List<DepartmentStaffBean>>>() {
                    override fun onSuccess(response: Response<HttpResult<List<DepartmentStaffBean>>>) {
                        val httpResult = response.body()
                        if (httpResult.status == 200 && httpResult?.items?.isNotEmpty() == true) {
                            val list = ArrayList<DepartmentStaffTreeBean>()
                            initDataList(parseTreeData(httpResult.items, list))
                        }
                    }
                })

    }


    private fun parseTreeData(dataList: List<DepartmentStaffBean>, dataTree: ArrayList<DepartmentStaffTreeBean>): ArrayList<DepartmentStaffTreeBean> {
        for (bean in dataList) {
            val treeBean = DepartmentStaffTreeBean()
            treeBean.content = bean.departName
            treeBean.id = bean.departId
            treeBean.isRoot = true
            dataTree.add(treeBean)
            if (bean.userInfo != null && bean.userInfo.size > 0) {
                val list = ArrayList<DepartmentStaffTreeBean>()
                for (userInfoBean in bean.userInfo) {
                    val treeBean1 = DepartmentStaffTreeBean()
                    treeBean1.id = userInfoBean.userGuid
                    treeBean1.icon = userInfoBean.photo
                    treeBean1.phone = userInfoBean.uname
                    treeBean1.content = userInfoBean.username
                    treeBean1.department = userInfoBean.department + "-" + userInfoBean.post
                    treeBean1.isRoot = false
                    list.add(treeBean1)
                }
                treeBean.childList = list
            }
            if (bean.childs != null && bean.childs.size > 0) {
                var list = ArrayList<DepartmentStaffTreeBean>()
                list = parseTreeData(bean.childs, list)
                treeBean.addChildList(list)
            }
        }
        return dataTree
    }

}
